package com.hopu.v1.service.front.question.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hopu.v1.pojo.question.ErrorNotebookOptions;
import com.hopu.v1.mapper.front.question.FErrorNotebookOptionsMapper;
import com.hopu.v1.pojo.exam.ExamOptionAnswer;
import com.hopu.v1.pojo.question.ErrorNotebookOptions;
import com.hopu.v1.pojo.question.ErrorNotebooksQuestions;
import com.hopu.v1.service.front.question.FErrorNotebooksQuestionsService;
import com.hopu.v1.mapper.front.question.FErrorNotebooksQuestionsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import com.hopu.v1.vo.JudgeErrQuestionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import java.util.List;

/**
 *
 */
@Service
public class FErrorNotebooksQuestionsServiceImpl extends ServiceImpl<FErrorNotebooksQuestionsMapper, ErrorNotebooksQuestions>
    implements FErrorNotebooksQuestionsService {

    @Autowired
    private FErrorNotebooksQuestionsMapper fErrorNotebooksQuestionsMapper;

    @Override
    public List<ErrorNotebooksQuestions> queryErrQuestions(String studentId) {
        ErrorNotebooksQuestions errorNotebooksQuestions = new ErrorNotebooksQuestions();
        errorNotebooksQuestions.setStatus(0);
        errorNotebooksQuestions.setStudentId(studentId);
        return fErrorNotebooksQuestionsMapper.selectAllByErrQuestion(errorNotebooksQuestions);
    }

    @Override
    public JudgeErrQuestionVo judgeErrQuestion(String studentId, int questionId, String studentAnswer) {
        ErrorNotebooksQuestions errorNotebooksQuestions = new ErrorNotebooksQuestions();
        errorNotebooksQuestions.setStatus(0);
        errorNotebooksQuestions.setStudentId(studentId);
        errorNotebooksQuestions.setId(questionId);
        List<ErrorNotebooksQuestions> questions = fErrorNotebooksQuestionsMapper.selectAllByErrQuestion(errorNotebooksQuestions);
        System.out.println("questions = " + questions);
        if(questions.size() == 0){
            return null;
        }
        if (questions.get(0).getQuestionType().equals("0") || questions.get(0).getQuestionType().equals("2")){
            for (ErrorNotebookOptions option : questions.get(0).getOptions()) {
                if (option.getStatus().equals("1")){
                    if (option.getId().equals(Integer.parseInt(studentAnswer))){
                        ErrorNotebooksQuestions errorNotebooksQuestions1 = new ErrorNotebooksQuestions();
                        errorNotebooksQuestions1.setStatus(1);
                        fErrorNotebooksQuestionsMapper.update(errorNotebooksQuestions1,new LambdaQueryWrapper<ErrorNotebooksQuestions>().eq(ErrorNotebooksQuestions::getId,questionId));
                        return new JudgeErrQuestionVo(questions.get(0),"回答正确");
                    }else {
                        ErrorNotebooksQuestions errorNotebooksQuestions1 = new ErrorNotebooksQuestions();
                        for (ErrorNotebookOptions errorNotebookOptions : questions.get(0).getOptions()) {
                            if (errorNotebookOptions.getId().equals(Integer.parseInt(studentAnswer)))
                                errorNotebooksQuestions1.setStudentAnswer(errorNotebookOptions.getOptions());
                        }
                        fErrorNotebooksQuestionsMapper.update(errorNotebooksQuestions1,new LambdaQueryWrapper<ErrorNotebooksQuestions>().eq(ErrorNotebooksQuestions::getId,questionId));
                        return new JudgeErrQuestionVo(questions.get(0),"回答错误");
                    }
                }
            }
        }else if (questions.get(0).getQuestionType().equals("1")){
            //多选题
            String[] split = studentAnswer.split(",");
            int trueCount = 0; //正确选项个数
            int studentTrueCount = 0; //学生正确选项个数
            for (ErrorNotebookOptions option : questions.get(0).getOptions()) {
                if (option.getStatus().equals("1")){
                    trueCount++;
                    for (String s : split) {
                        if (option.getId().equals(Integer.parseInt(s))){
                            studentTrueCount++;
                        }
                    }
                }

            }
            if (trueCount == studentTrueCount&&split.length==trueCount){
                ErrorNotebooksQuestions errorNotebooksQuestions1 = new ErrorNotebooksQuestions();
                errorNotebooksQuestions1.setStatus(1);
                fErrorNotebooksQuestionsMapper.update(errorNotebooksQuestions1,new LambdaQueryWrapper<ErrorNotebooksQuestions>().eq(ErrorNotebooksQuestions::getId,questionId));
                return new JudgeErrQuestionVo(questions.get(0),"回答正确");
            }else {
                ErrorNotebooksQuestions errorNotebooksQuestions1 = new ErrorNotebooksQuestions();
                StringBuilder answer = new StringBuilder();
                for (ErrorNotebookOptions errorNotebookOptions : questions.get(0).getOptions()) {
                    for (String s : split) {
                        if (errorNotebookOptions.getId().equals(Integer.parseInt(s)))
                           answer.append(errorNotebookOptions.getOptions()).append(",");
                    }
                }
                errorNotebooksQuestions1.setStudentAnswer(answer.substring(0,answer.length()-1).toString());
                fErrorNotebooksQuestionsMapper.update(errorNotebooksQuestions1,new LambdaQueryWrapper<ErrorNotebooksQuestions>().eq(ErrorNotebooksQuestions::getId,questionId));
                return new JudgeErrQuestionVo(questions.get(0),"回答错误");
            }
        }else {
            return new JudgeErrQuestionVo(questions.get(0),"不支持该类型题目判断");
        }
        return null;
    }
    @Autowired
    private FErrorNotebooksQuestionsMapper errorNotebooksQuestionsMapper;
    @Autowired
    private FErrorNotebookOptionsMapper errorNotebookOptionsMapper;

    /**
     * 批量添加错题及选项
     * @param errorNotebooksQuestionsList
     */
    @Transactional
    @Override
    public void addErrorExam(List<ErrorNotebooksQuestions> errorNotebooksQuestionsList) {
        System.out.println(errorNotebooksQuestionsList);
        // 批量插入题目
        errorNotebooksQuestionsMapper.insert(errorNotebooksQuestionsList);
        // 批量插入选项
        List<ErrorNotebookOptions> allOptions = new ArrayList<>();
        for (ErrorNotebooksQuestions question : errorNotebooksQuestionsList) {
            if (question.getOptions() != null) {
                for (ErrorNotebookOptions option : question.getOptions()) {
                    option.setErrorNotebookId(question.getId());
                    allOptions.add(option);
                }
            }
        }

        // 批量插入选项
        if (!allOptions.isEmpty()) {
            errorNotebookOptionsMapper.insert(allOptions);
        }
    }
}




